﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Services.Interface;
using HanGu.Entity;
using HanGu.Common;

namespace Hangu.WebClient.Controllers.Admin
{
    public class ChargeRecordeController : AdminBaseController
    {
        private static readonly IChargeRecorde servicCharge = ServiceFactory.GetService<IChargeRecorde>();
        private static readonly IMemberInfo serviceMemberInfo = ServiceFactory.GetService<IMemberInfo>();
        private static readonly IDictionary serviceDict = ServiceFactory.GetService<IDictionary>();

        //
        // GET: /ChargeRecorde/

        public ActionResult Index(int id)
        {
            base.ExceptionHand(new Exception("test errot"));
            return View(viewList, GetChargeRecordeList(id));
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {

            int id = 0;
            int.TryParse(Request["VIPID"], out id);
            return View(viewList, GetChargeRecordeList(id));
        }

        List<VChargeRecorde> GetChargeRecordeList(int id)
        {
            SearchParma.IntObj1 = id;
            List<VChargeRecorde> chargeRecordeList = servicCharge.GetAll(SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return chargeRecordeList;
        }

        SelectList GetListFromDict(string groupName, bool IsSelectLabel = false)
        {
            var lst = serviceDict.GetByGroupName(groupName);
            if (IsSelectLabel)
            {
                lst.Insert(0, new Dictionary() { key = string.Empty, Value = "-----------请选择------------" });
            }
            SelectList sList = new SelectList(lst, "GenerateKey", "Value");
            return sList;
        }

        public ActionResult Create(int id)
        {
            //ViewData["Degree"] = GetListFromDict("Degree");
            ViewData["MemberInfo"] = TempData["MemberInfo"] = serviceMemberInfo.GetByID(id);
            ChargeRecorde chargeRecorde = new ChargeRecorde() { VIPID = id, ChangeTime = DateTime.Now, Changer = CurentUser.UserName };
            return View(viewCreate, chargeRecorde);
        }

        //
        // POST: /ChargeRecorde/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            ChargeRecorde chargeRecorde = new ChargeRecorde();
            try
            {
                var memberinfo = TempData["MemberInfo"] as MemberInfo;
                if (memberinfo != null)
                {
                    TryUpdateModel(chargeRecorde, collection);
                    if (servicCharge.Add(chargeRecorde))
                    {
                        memberinfo.Charge = memberinfo.Charge-chargeRecorde.ChangeValue;
                        serviceMemberInfo.Update(memberinfo);
                        navTabId = "MemberInfo";
                        ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");
                    }
                    else
                    { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); }
                }
                else
                { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";
        }

        //
        // GET: /ChargeRecorde/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /ChargeRecorde/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ChargeRecorde/Delete/5
        [HttpPost]
        public string Delete(int id, string guid)
        {
            try
            {                
                servicCharge.Delete(id);
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";

        }
    }
}
